package com.api.gbhg.luoao.la.util;

import cn.hutool.core.util.StrUtil;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础工具类
 */
public class Allutils {




    //根据requestid查询workflowid
    public static String getworkfolwid(String requestId){

        if (StrUtil.isNotEmpty(requestId)) {
            RecordSet rs = new RecordSet();
            rs.executeQuery("select workflowid from workflow_requestbase where requestid=" + requestId);
            if (rs.next()) {
                return rs.getString("workflowid");
            }
        }

       return  null;

    }

    //根据requestid查询流程当前所在节点id
    public static String getnodelid(String requestId){

        if (StrUtil.isNotEmpty(requestId)) {
            RecordSet rs = new RecordSet();
            rs.executeQuery("select currentnodeid from workflow_requestbase where requestid=" + requestId);
            if (rs.next()) {
                return rs.getString("currentnodeid");
            }
        }

        return  null;

    }


    //查询节点中文名称
    public static  String getnodename(String nodelid){
        if (StrUtil.isNotEmpty(nodelid)){
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from workflow_nodebase where id = "+nodelid);
            rs.next();
            return  rs.getString("nodename");
        }
        return "";
    }


    //根据loginid查询userid
    public static String userid(String loginid){

        if (StrUtil.isNotEmpty(loginid)){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from hrmresource where loginid='"+loginid+"'");
        rs.next();
        return rs.getString("id");
        }
        return  null;
    }



    //根据id查询用户名称
    public static String UserName(String id){
        if (StrUtil.isNotEmpty(id)){
            RecordSet rs = new RecordSet();
            //查询普通人员表
            rs.executeQuery("select lastname from hrmresource where id = ?",id);
            while (rs.next()){
                return rs.getStringNoTrim("lastname");

            }

            //查询系统管理员表
            rs.executeQuery("select lastname from hrmresourcemanager where id =?",id);
            while (rs.next()){
                return rs.getStringNoTrim("lastname");
            }
        }
        return null;

    }

    //根据formid查询流程明细表名
    public static List<String> getDetailtableName(String formid){

        if (StrUtil.isNotEmpty(formid)) {
            List<String> list = new ArrayList<>();
            RecordSet rs = new RecordSet();
            rs.executeQuery("select tablename from workflow_billdetailtable  where billid =" + formid+"  order by id");
            while (rs.next()) {
                list.add(rs.getStringNoTrim("tablename"));
            }
            return list;
        }
        return  null;
    }


    //根据workflowid查询formid
    public static String getFormid(String workflowId){
        if (StrUtil.isNotEmpty(workflowId)) {
            RecordSet rs = new RecordSet();
            rs.executeQuery("select formid from workflow_base where id=" + workflowId);
            rs.next();
            return rs.getStringNoTrim("formid");
        }
        return null;

    }

    //根据workflowid查询主表名
    public static String getMaintableName(String workflowId){

        if (StrUtil.isNotEmpty(workflowId)){

            String formid = Allutils.getFormid(workflowId);
            if (StrUtil.isNotEmpty(formid)){
                RecordSet rs = new RecordSet();
                rs.executeQuery("select tablename  from workflow_bill where id="+formid);
                rs.next();
                return rs.getString("tablename");
            }
            return null;

        }

        return null;

    }

    //获取mainid
    public static String getMainid(String requestid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from "+getMaintableName(getworkfolwid(requestid))+" where requestid="+requestid);
        rs.next();
        return rs.getString("id");
    }



}
